package com.chenyi.data.structure.queue;

import java.awt.*;

/**
 * @author by chenyi
 * @className ArrayQueue
 * @date 2021/12/16 18:54
 */

public class ArrayQueue implements Queue{

    /**
     * 队列最大容量
     */
    private int maxSize;

    /**
     * 队列头
     */
    private int front;

    /**
     * 队列尾
     */
    private int rear;

    /**
     * 存放队列数据
     */
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        this.front = -1;
        this.rear = -1;
    }

    /**
     * 判断队列是否已满
     * @return
     */
    @Override
    public boolean isFull() {
        return this.rear == maxSize - 1;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    @Override
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    /**
     * 添加队列
     * @param e
     * @return
     */
    @Override
    public boolean addQueue(int e) {
        // 判断队列是否已满
        if (isFull()) {
            System.out.println("队列已满无法添加队列");
            return false;
        }
        this.arr[++this.rear] = e;
        return true;
    }

    /**
     * 获取队列
     * @return
     */
    @Override
    public int getQueue() {
        // 判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空无法获取数据");
        }
        return arr[++this.front];
    }

    /**
     * 展示队列
     */
    @Override
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空无法获取数据");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("queue[%d]=%d\n", i, arr[i]);
        }
    }

    /**
     * 显示队列的头数据
     * @return
     */
    @Override
    public int peekHeadQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空无法获取数据");
        }
        return this.arr[front + 1];
    }


}
